/**
 * Description: 登录
 *
 * @author      Eden
 * @datetime    2016/11/25 09:30
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {
    "use strict";

    var api = {
        bankUrl: "/money/bank/info",
        checkOrderUrl: "/money/purchaser/checkOrder"
    };

    sm.controller('admin-money-purchaser-apply', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        var startYear = 2017;
        var d = new Date();
        var thisYear = d.getFullYear();
        var thisMonth = d.getMonth() + 1;
        $scope.baseId = 0;
        $scope.selectedYear = thisYear;
        $scope.selectedMonth = thisMonth;
        $scope.years = [];
        $scope.monthes = [];
        if (thisYear >= startYear) {
            for(var i = startYear; i <= thisYear; i++) {
                $scope.years.push(i);
            }
        }

        if ($scope.selectedYear == thisYear) {
            for(var i = thisMonth; i >= 1; i--) {
                $scope.monthes.push(i);
            }
        }

        $scope.$watch('selectedYear', function (newVal, oldVal) {
            if (newVal === oldVal) {
                return false;
            }

            $scope.monthes = [];
            if ($scope.selectedYear < thisYear) {
                for (var i = 12; i >= 1; i--) {
                    $scope.monthes.push(i);
                }
            }

            if ($scope.selectedYear == thisYear) {
                for(var i = thisMonth; i >= 1; i--) {
                    $scope.monthes.push(i);
                }
            }
        });

        $scope.selectYear = function (index) {
            $scope.selectedYear = $scope.years[index];
        };

        $scope.selectMonth = function (index) {
            $scope.selectedMonth = $scope.monthes[index];
        };

        $scope.has = false;
        $scope.result = {};
        $scope.bank = {};
        $scope.checkOrder = function () {

            $http.get(api.checkOrderUrl, {
                params: {
                    year: $scope.selectedYear,
                    month: $scope.selectedMonth
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.has = true;
                    $scope.result = res.data;
                    $scope.baseId = res.baseId;

                    if ($scope.selectedMonth == thisMonth && $scope.selectedYear == thisYear) {
                        $scope.showApplyBtn = false;
                    } else {
                        $scope.showApplyBtn = true;
                    }
                }
            });
        };

        $scope.getBank = function () {
            $http.get(api.bankUrl).success(function (res) {
                if (res.state) {
                    $scope.bank = res.data;
                }
            });
        };

        $scope.getBank();

        $scope.alertModal = function () {
            if (!$scope.bank || !$scope.bank.bankname) {
                Modal.alert({
                    'title': "操作提示",
                    'content': "您还没有完善银行卡信息...",
                    'buttons': [{
                        name: "取消"
                    }, {
                        name: "前往设置",
                        event: function () {
                            location.href = "/money/bank/index"
                        }
                    }]
                });
                return false;
            }


            /*
            if ($scope.selectedMonth == thisMonth && $scope.selectedYear == thisYear) {
                Modal.alert({
                    title: "操作提示",
                    content: "当月订单只允许查看，只能申请上月"
                });
                return false;
            }
            */

            location.href = '/money/purchaser/check?year='+ $scope.selectedYear +'&month=' + $scope.selectedMonth;
        }

    }]);

})();

